Skip to content

feat: Integrate coolbpf cpu profiling feature in loongcollector#2391

Open
wokron wants to merge 99 commits into
alibaba:mainfrom
wokron:support-coolbpf-cpu-profiling
Open

feat: Integrate coolbpf cpu profiling feature in loongcollector#2391
wokron wants to merge 99 commits into
alibaba:mainfrom
wokron:support-coolbpf-cpu-profiling

Conversation

@wokron
Copy link
Copy Markdown

@wokron wokron commented Sep 18, 2025

No description provided.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Sep 18, 2025

CLA assistant check
All committers have signed the CLA.

@yyuuttaaoo yyuuttaaoo marked this pull request as ready for review November 6, 2025 02:17
Copilot AI review requested due to automatic review settings November 6, 2025 02:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR integrates coolbpf CPU profiling capabilities into loongcollector by adding a new input_cpu_profiling plugin. The implementation enables continuous CPU profiling of specified processes through command-line pattern matching and container discovery.

Key changes:

  • New CPU profiling plugin with process discovery mechanism
  • Integration with coolbpf profiler library
  • Plugin registration and lifecycle management

Reviewed Changes

Copilot reviewed 39 out of 39 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
core/plugin/input/InputCpuProfiling.{h,cpp} New plugin implementation for CPU profiling input
core/ebpf/plugin/cpu_profiling/* Core CPU profiling manager and process discovery logic
core/ebpf/driver/CpuProfiler.h Wrapper for coolbpf profiler library integration
core/ebpf/Config.{h,cpp} CPU profiling configuration option handling
core/ebpf/include/export.h Type definitions for CPU profiling
core/unittest/input/InputCpuProfilingUnittest.cpp Unit tests for the input plugin
core/unittest/ebpf/*Unittest.cpp Unit tests for CPU profiling components
Various CMakeLists.txt Build system updates for new components
Comments suppressed due to low confidence (1)

core/unittest/input/InputCpuProfilingUnittest.cpp:1

  • Corrected spelling of 'CommandLines' to 'CommandLines' in comment context.

Comment thread core/ebpf/driver/CpuProfiler.h
Comment on lines +265 to +266
mEBPFAdapter->UpdatePlugin(PluginType::CPU_PROFILING,
buildCpuProfilingConfig(std::move(totalPids), std::nullopt, nullptr, nullptr));
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing null handler and context to buildCpuProfilingConfig during update will overwrite the valid handler set during initialization, breaking profiling event handling.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看了代码,这是false-positive,但建议加一下注释,说明只能更新pid,不能更新其他,或者单独弄一个buildUpdateCpuProfilingConfig

Comment thread core/ebpf/plugin/cpu_profiling/ProcessDiscoveryManager.cpp Outdated
Comment thread core/ebpf/plugin/cpu_profiling/CpuProfilingManager.cpp Outdated
Comment thread core/ebpf/plugin/cpu_profiling/CpuProfilingManager.cpp
Comment thread core/ebpf/driver/CpuProfiler.h Outdated
@wokron wokron force-pushed the support-coolbpf-cpu-profiling branch from ecf5b83 to a3a8e56 Compare November 7, 2025 01:48
@wokron wokron force-pushed the support-coolbpf-cpu-profiling branch from c00d080 to 0f58800 Compare November 27, 2025 09:24
@wokron wokron force-pushed the support-coolbpf-cpu-profiling branch 5 times, most recently from 2a5948d to 6c51221 Compare December 22, 2025 02:21
@yyuuttaaoo yyuuttaaoo self-requested a review February 24, 2026 06:53
Comment thread core/container_manager/ContainerManager.cpp Outdated
Comment thread core/plugin/input/InputCpuProfiling.h
Comment thread core/plugin/input/InputCpuProfiling.cpp Outdated
Comment thread core/ebpf/plugin/cpu_profiling/ProcessDiscoveryManager.h Outdated
Comment thread core/ebpf/plugin/cpu_profiling/ProcessDiscoveryManager.h Outdated
Comment thread core/ebpf/plugin/cpu_profiling/CpuProfilingManager.cpp Outdated
Comment thread core/ebpf/plugin/cpu_profiling/CpuProfilingManager.cpp Outdated
Comment thread core/ebpf/plugin/cpu_profiling/CpuProfilingManager.cpp Outdated
Comment thread core/container_manager/ContainerManager.h Outdated
Comment thread core/container_manager/ContainerManager.cpp Outdated
@yyuuttaaoo
Copy link
Copy Markdown
Collaborator

使用文档仿照其他插件补充一下

Comment thread core/ebpf/plugin/cpu_profiling/CpuProfilingManager.cpp Outdated
Comment thread core/ebpf/driver/CpuProfiler.h Outdated
Comment thread core/ebpf/driver/CpuProfiler.h Outdated
std::lock_guard<std::mutex> lock(mMutex);
if (mProfiler == nullptr) {
livetrace_enable_tracing();
mProfiler = livetrace_profiler_create();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有没有控制队列大小等资源相关的参数,如何约束其资源使用量呢

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

队列大小的控制是 coolbpf 侧提供的。其中根据 profile 周期确定了有界队列的大小。https://gitee.com/anolis/coolbpf/blob/master/src/profiler/src/probes/probes.rs#L274

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看了下,coolbpf 侧没有根据 profile 周期设置有界队列。队列是 unbounded,理论上如果消费者(LoongCollector Poll)跟不上生产速度,channel 会无限增长。队列大小控制完全不在 coolbpf 这一侧。

Copy link
Copy Markdown
Author

@wokron wokron May 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

链接给错行数了 :( ,应该是下面 284 行

let ms = profile_period() as usize;
let sample_per_sec = 1000 / ms;
let ten_sec_samples = sample_per_sec * 10 * num_possible_cpus().unwrap_or(1);
log::info!("cache max stack samples: {}", ten_sec_samples);
let (tx, rx) = crossbeam_channel::bounded(ten_sec_samples);

其中 crossbeam_channel::bounded(ten_sec_samples); 创建一个有界队列,长度为 ten_sec_samples

发送端 tx 传给了 thread_poll_trace_eventthread_poll_report_event 这两个 callback,分别在两个线程中 poll perf buffer。

接收端 rx 传给了 Probe。最后 livetrace_profiler_read 就是从接收端读取的数据。

@wokron wokron force-pushed the support-coolbpf-cpu-profiling branch 2 times, most recently from 37e01d9 to 8191f24 Compare March 4, 2026 03:00
@wokron
Copy link
Copy Markdown
Author

wokron commented Mar 6, 2026

使用文档仿照其他插件补充一下

文档已经补充了

@Takuka0311
Copy link
Copy Markdown
Collaborator

Takuka0311 commented Apr 27, 2026

百炼自动化审查:建议保持开启。

本 PR 为 LoongCollector 新增基于 coolbpf 的 CPU 性能剖析插件(input_cpu_profiling),包含核心 C++ 实现、eBPF 驱动适配、进程发现管理器、单元测试与中文文档。当前处于活跃审查阶段,维护者于 2026-05-12 提出多项代码质量与鲁棒性改进意见,作者已积极回应并修复部分问题,最新提交于 2026-05-18 推送。PR 当前可合并,需保持开启以待审查完成与合并。.

最佳落地路径:

建议作者继续跟进维护者 2026-05-12 的剩余审查意见(如 parseStackCnt 解析鲁棒性增强、Poll 空 PID 尾部数据 drain 逻辑、/proc 扫描频率优化及补充缺失单元测试),待 CI 通过且审查批准后由维护者合并。.

已核对内容:

  • active_review: 维护者 yyuuttaaoo 于 2026-05-12 提出 parseStackCnt 鲁棒性、Poll 空 PID 丢数据、/proc 扫描开销及 UT 缺失等审查意见;作者 wokron 于 05-13/05-14 积极回应,改用 Json::Value 处理转义,移除 GetContainerHostPath,补充 ContainerManager UT,并向上游 coolbpf 提交 API 改进 PR。. (core/ebpf/plugin/cpu_profiling/CpuProfilingManager.cpp:1, 9b0716c2831a)
  • mergeable_state: PR 当前 mergeable 状态为 true,已解决先前合并冲突,可进入最终审查与合并流程。. (eef380de68cb)
  • feature_addition: 新增 input_cpu_profiling 插件核心实现、eBPF 驱动适配(CpuProfiler)、进程发现管理器及完整单元测试与中文文档,属于有效且结构完整的新功能贡献。. (core/plugin/input/InputCpuProfiling.cpp:1, 9b0716c2831a)

百炼审查备注:模型 qwen3.6-max-preview;对照提交 b42ea269f84a

StringView symbolView(symbol);
StringViewSplitter splitter(symbolView, "\n");
for (const auto& line : splitter) {
auto pos1 = line.find(';');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 问题: parseStackCnt 用第一个 ; 分割 <comm>:<pid> 与栈,若 comm 含 ; 则全行解析错误
  • 影响: /proc/*/comm 可含特殊字符(虽少见),解析错位会导致栈数据错乱或静默丢弃。
  • 建议: 增强解析鲁棒性——从右侧匹配 :<pid>;,或在注释/文档中明确 coolbpf 的格式契约并加校验。

Comment thread core/ebpf/driver/CpuProfiler.cpp Outdated
Comment thread core/ebpf/plugin/cpu_profiling/ProcessEntry.h Outdated
Comment thread core/ebpf/plugin/cpu_profiling/ProcessDiscoveryManager.h Outdated
Comment thread core/container_manager/ContainerManager.h
Comment thread core/container_manager/ContainerManager.h
Comment thread docs/cn/plugins/input/native/input-cpu-profiling.md Outdated
Comment thread core/ebpf/plugin/cpu_profiling/CpuProfilingManager.cpp Outdated
@wokron wokron force-pushed the support-coolbpf-cpu-profiling branch from 54222e9 to 9f1231f Compare May 14, 2026 09:43
@wokron wokron force-pushed the support-coolbpf-cpu-profiling branch from 9f1231f to 9b0716c Compare May 18, 2026 06:09
@wokron wokron force-pushed the support-coolbpf-cpu-profiling branch from eef380d to dd8648e Compare May 19, 2026 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants